<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\facade\Request;
use think\Validate;
use app\admin\model\Teacher as TeacherModel;
use app\admin\controller\Section;
class Leave extends Common
{
    /**请假列表
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function lists(){
        if (request()->isAjax()) {

            input('post.keyword')?$keyword = input('post.keyword'):$keyword ='';
            input('post.leave_date')?$leave_date = input('post.leave_date'):$leave_date ='';
            $page =input('page',1);
            $pageSize =input('limit',config('pageSize'));

            $time1 = strtotime(date('Y-m-d'));
            $time2 = strtotime(date('Y-m-d',strtotime('+1 day')))-1;

            if($leave_date!=''){
                $time1 = strtotime($leave_date);
                $time2   = $time1+(24*60*60)-1;
                $time_where = "s.leave_date_start <= $time1 AND s.leave_date_end >= $time2";
            }else{
                $time_where = "s.leave_date_start <= $time1 AND s.leave_date_end >= $time2";
            }

            if($keyword!=''){
                $time_where = "";
            }


            $list = Db::name('sign_leave')->alias('s')
                ->join('teacher t','s.teacher_id=t.id','left')
                ->field('s.*,t.teacher_no, t.true_name')
                ->where('s.is_del',0)
                ->where('t.true_name|t.teacher_no','like',"%$keyword%")
                ->where($time_where)
                ->order('s.create_time asc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();

            if (!empty($list)) {
                foreach ($list['data'] as $k => $v) {
                    $list['data'][$k]['review_time'] = toDate($v['review_time']);
                    $list['data'][$k]['leave_date'] = toDate($v['leave_date_start'],'Y-m-d').' 至 '.toDate($v['leave_date_end'],'Y-m-d');
                }
            }

            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }

        return $this->fetch('leave_list');
    }


    /**请假状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function leaveState()
    {
        $leave_id = input('post.id');
        $status = input('post.status');

        if (empty($leave_id)) {
            $this->error('请假ID不存在');
        }

        $ret = Db::name('sign_leave')->where('id', $leave_id)->update(['status' => $status,'reviewer'=>session('username'),'review_time'=>time()]);
        $true_name = Db::name('sign_leave')->alias('s')
            ->join('teacher t','s.teacher_id=t.id')
            ->where('s.id', $leave_id)
            ->value('t.true_name');

        $log = [
            'content' => '修改请假状态' . $true_name
        ];

        $wed = new Weblog();
        $res = $wed->insert($log);

        if ($ret && $res) {
            $this->success('操作成功',url('lists'));
        }else {
            $this->error('操作失败!');
        }
    }


    /**
     * 删除请假
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function leaveDel(){

        $id=input('post.id');

        if (session('aid')==1){

            $ret = Db::name('sign_leave')->where('id', $id)->update(['is_del' => 1]);

            $true_name = Db::name('sign_leave')->alias('s')
                ->join('teacher t','s.teacher_id=t.id')
                ->where('s.id', $id)
                ->value('t.true_name');

            $log = [
                'content'=>'删除请假记录'.$true_name
            ];

            $wed = new Weblog();
            $res = $wed->insert($log);

            if ($ret && $res) {
                return $result = ['code'=>1,'msg'=>'删除成功!'];
            }else {
                $this->error('删除失败!');
            }

        }else{
            $this->error('您没有删除请假记录的权限!');
        }
    }


    /**后台添加请假教师
     * @return mixed
     */
    public function leaveAdd(){

        if (request()->isAjax()) {

            $data = input('post.');
            $leave = explode(' - ',$data['leave_date']);
            $data['leave_date_start'] = strtotime($leave[0]);
            $data['leave_date_end'] = (strtotime($leave[1]))+(24*60*60)-1;
            unset($data['leave_date']);
            $data['create_time'] = time();

            $ret = Db::name('sign_leave')->insert($data);
            $true_name = Db::name('teacher')->where('id', $data['teacher_id'])->value('true_name');

            $log = [
                'content'=>'添加请假人：'.$true_name
            ];

            $wed = new Weblog();
            $res = $wed->insert($log);

            if ($ret && $res) {
                $this->success("添加成功",'teacher/lists');
            }else {
                $this->error('添加失败!');
            }

        }

        $this->assign('teacher_id',input('id'));
        return $this->fetch('leaveForm');

    }

}